﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace Barbar.MapReport.Common {
  [Serializable]
  public class BoundD {
    private PointD m_TopLeft;
    private PointD m_BottomRight;

    public PointD TopLeft {
      get { return m_TopLeft; }
    }

    public PointD BottomRight {
      get { return m_BottomRight; }
    }

    public BoundD(IGeoBound geoBound) {
      PointD firstPoint = MercatorProjection.DefaultProjection.ToPointD(geoBound.Northeast);
      PointD secondPoint = MercatorProjection.DefaultProjection.ToPointD(geoBound.Southwest);
      m_TopLeft = new PointD(Math.Min(firstPoint.X, secondPoint.X), Math.Min(firstPoint.Y, secondPoint.Y));
      m_BottomRight = new PointD(Math.Max(firstPoint.X, secondPoint.X), Math.Max(firstPoint.Y, secondPoint.Y));
    }

    public override string ToString() {
      return String.Format(CultureInfo.InvariantCulture, "{0} - {1}", m_TopLeft, m_BottomRight);
    }

    public static bool Intersect(BoundD firstBound, BoundD secondBound) {
      return
        !(firstBound.TopLeft.Y > secondBound.BottomRight.Y ||
        firstBound.BottomRight.X < secondBound.TopLeft.X ||
        firstBound.BottomRight.Y < secondBound.TopLeft.Y ||
        firstBound.TopLeft.X > secondBound.BottomRight.X);
    }
  }
}
